using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace Framework
{

    [CustomEditor(typeof(UGUIWindow))]
    public class UGUIWindowEditor : UGUIWidgetEditor
    {
        protected override void OnDraw()
        {
            base.OnDraw();
            EditorGUILayout.Space(10);
            SerializedProperty destroyTime = serializedObject.FindProperty("destroyTime");
            SerializedProperty layer = serializedObject.FindProperty("layer");
            SerializedProperty opaqueView = serializedObject.FindProperty("opaqueView");
            SerializedProperty safeAreaTransform = serializedObject.FindProperty("safeAreaTransform");
            SerializedProperty showState = serializedObject.FindProperty("showState");
            SerializedProperty scaleTransform = serializedObject.FindProperty("scaleTransform");

            if (EditorApplication.isPlaying)
            {
                GUILayout.Label("显示层级： " + layer.intValue);
            }
            else
            {
                var processors = GetProcessor();
                if (processors.Count > 0)
                {
                    var names = processors[0].GetLayerNames();
                    if (names != null && names.Length > 0)
                    {
                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Label("显示层级： ", GUILayout.Width(60f));

                        EditorGUI.BeginChangeCheck();
                        layer.intValue = EditorGUILayout.Popup(layer.intValue, names, GUILayout.MaxWidth(80f));
                        if (EditorGUI.EndChangeCheck())
                        {

                        }
                        EditorGUILayout.EndHorizontal();
                    }
                }
                else
                {
                    EditorGUILayout.BeginHorizontal();
                    GUIStyle myStyle = new GUIStyle(GUI.skin.label);
                    myStyle.normal.textColor = Color.red;
                    GUILayout.Label("显示层级：", GUILayout.Width(60f));
                    EditorGUILayout.EndHorizontal();
                }
            }

            EditorGUILayout.BeginHorizontal();

            EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
            GUILayout.Label("不透明: ", GUILayout.Width(50f));
            opaqueView.boolValue = EditorGUILayout.Toggle(opaqueView.boolValue);

            EditorGUI.EndDisabledGroup();

            GUILayout.Label("销毁时间: ", GUILayout.Width(60f));
            destroyTime.floatValue = EditorGUILayout.FloatField(destroyTime.floatValue);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.PropertyField(safeAreaTransform, new GUIContent("安全区域对象"));
            EditorGUILayout.PropertyField(showState, new GUIContent("显隐状态"));
            EditorGUILayout.PropertyField(scaleTransform, new GUIContent("适配缩放对象"));

        }
        protected override void OnEnable()
        {
            base.OnEnable();
            var Window = target as UGUIWindow;
            var canvas = Window.GetComponent<Canvas>();
            if (canvas != null)
            {
                canvas.overrideSorting = true;
            }
        }


    }
}